define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'flow/flow/index',
                    add_url: 'flow/flow/add',
                    edit_url: 'flow/flow/edit',
                    del_url: 'flow/flow/del',
                    wfjk_url: 'flow/flow/wfjk',
                    wfstart_url: 'flow/flow/wfstart',
                    multi_url: 'flow/flow/multi',
                    table: 'flow',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type_text', title: __('Type')},
                        {field: 'flow_name', title: __('Flow_name')},
                        {field: 'flow_desc', title: __('Flow_desc')},
                        {field: 'dep_text', title: __('部门/分公司'), operate:false,formatter: Table.api.formatter.label},
                        {field: 'add_time', title: __('Add_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'edit', title: __('设计流程'),operate:false,formatter:function (value, row, index) {
                            if(row.edit == null)
                            {
                              return '<a href="/admin/flow/flow/desc/flow_id/'+row.id+'" class="btn btn-xs btn-success btn-addtabs" title="设计流程" data-table-id="table" data-field-index="8" data-row-index="0" data-button-index="0"><i class="fa fa-sticky-note"></i> 设计流程</a>';
                            }
                            return '<a href="/admin/flow/flow/desc/flow_id/'+row.id+'" class="btn btn-xs btn-success btn-addtabs disabled" title="设计流程" data-table-id="table" data-field-index="8" data-row-index="0" data-button-index="0"><i class="fa fa-sticky-note"></i> 运行中...</a>';
                          }},
                        {field: 'status', title: __('禁用'), searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        wfjk:function () {
            Table.api.init({
              extend: {
                index_url: 'flow/flow/wfjk',
                run_url: 'flow/flow/run',//待审
                multi_url:'flow/flow/stop',//终止
              }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
              url: $.fn.bootstrapTable.defaults.extend.index_url,
              pk: 'id',
              sortName: 'id',
              columns: [
                [
                  {checkbox: true},
                  {field: 'id', title: __('工作流编号')},
                  {field: 'from_table_comment', title: __('工作流类型')},
                  {field: 'flow_name', title: __('工作流名称')},
                  {field: 'status', title: __('当前状态'),formatter:function (value,row,index ) {
                      if(value == 0)
                      {
                        return '未审核';
                      }
                      return '已审核'
                    }},
                  {field: 'user', title: __('业务办理人')},
                  {field: 'dateline', title: __('业务描述'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                  {field: 'operate', title: __('Operate'), table: table,
                    buttons: [
                      {name: 'stop', text: '终止', title: '终止', icon: 'fa fa-pause', classname: 'btn btn-xs btn-danger btn-change btn-stop'},
                      {name: 'run', text: '代审', title: '代审', icon: 'fa fa-play', classname: 'btn  btn-xs btn-success btn-change btn-run-amor'},
                    ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                ]
              ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        wfstart:function () {
          Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});